<?php

declare(strict_types=1);

namespace App\Controller\Common;

use App\Utils\Annotation\ApiName;
use App\Utils\Annotation\ApiParam;
use App\Utils\Traits\ControllerTrait;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class DefaultController
 * @package App\Controller\Common
 * @Route("/common", name="common_")
 * @ApiName(name="公共")
 */
class DefaultController extends AbstractController {
    use ControllerTrait;

    private $logger;

    public function __construct (LoggerInterface $adapayLogger) {
        $this->logger = $adapayLogger;
    }

    /**
     * @ApiName(name="adaPay 日志接收")
     * @ApiParam(name="name", type="string", comment="名称", require=true)
     * @ApiParam(name="level", type="string", comment="级别", require=true)
     * @ApiParam(name="message", type="string", comment="内容", require=true)
     * @Route("/adapay/log", methods={"POST"}, name="adapay_log")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function adaPayLog (Request $request) {
        $params = $this->onlyDeclare($request);
        $level = $params['level'];
        if (!method_exists($this->logger, $params['level'])) {
            $level = 'info';
        }
        $this->logger->$level('类型: '.$params['name'].' - '.$params['message']);
        return json_success();
    }
}